package de.wbh.gwt.form.layouter.shared.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.inject.client.AsyncProvider;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;

import de.wbh.gwt.form.layouter.shared.place.CreateConfigurationPlace;
import de.wbh.gwt.form.layouter.shared.place.LoadPlace;

public class StartActivity extends AbstractActivity implements StartView.Presenter {

	private final AsyncProvider<StartView> viewProvider;
	private final PlaceController placeController;

	private StartView view;

	@Inject
	public StartActivity(AsyncProvider<StartView> viewProvider, PlaceController placeController) {
		this.viewProvider = viewProvider;
		this.placeController = placeController;
	}

	@Override
	public void gotoCreate() {
		this.placeController.goTo(CreateConfigurationPlace.create());
	}

	@Override
	public void gotoLoad() {
		this.placeController.goTo(new LoadPlace());
	}

	@Override
	public void start(final AcceptsOneWidget panel, EventBus eventBus) {
		this.viewProvider.get(new AsyncCallback<StartView>() {

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(StartView result) {
				StartActivity.this.view = result;
				StartActivity.this.view.setPresenter(StartActivity.this);
				panel.setWidget(StartActivity.this.view);
			}
		});
	}

}
